package org.nbict.iot.persist.repo;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.nbict.iot.PropertyUtil;
import org.nbict.iot.persist.Repo;
import org.nbict.iot.task.once.TimeSpan;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: lilingyan
 */
public class MysqlRepo implements Repo {

    private String url;
    private String username;
    private String password;
    private String driverClassName;

    private int initialSize;
    private int minIdle;
    private int maxActive;

    DruidDataSource dataSource;

    private String insertSQL;

    public MysqlRepo() {
        init();
    }

    private void init() {
        url = PropertyUtil.getProperty("jdbc.url");
        username = PropertyUtil.getProperty("jdbc.username");
        password = PropertyUtil.getProperty("jdbc.password");
        driverClassName = PropertyUtil.getProperty("jdbc.driverClassName");

        initialSize = Integer.valueOf(PropertyUtil.getProperty("jdbc.datasource.initialSize").trim());
        minIdle = Integer.valueOf(PropertyUtil.getProperty("jdbc.datasource.minIdle").trim());
        maxActive = Integer.valueOf(PropertyUtil.getProperty("jdbc.datasource.maxActive").trim());

        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setInitialSize(initialSize);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxActive(maxActive);

        insertSQL = "INSERT INTO car_run_journey (\n" +
                "  `car_vin`,\n" +
                "  `from`,\n" +
                "  `to`,\n" +
                "  `soc`,\n" +
                "  `fromlon`,\n" +
                "  `fromlat`,\n" +
                "  `tolon`,\n" +
                "  `tolat`,\n" +
                "  `status`\n" +
                ")\n" +
                "VALUES\n" +
                "  ( ?, ?, ?, ?, ?, ?, ?, ?, ? )";

    }

    public void insertJourney(List<TimeSpan> times){
        DruidPooledConnection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);
            PreparedStatement psts = connection.prepareStatement(insertSQL);

            for(TimeSpan  time : times) {
                psts.setString(1, time.getVin());
                psts.setLong(2, time.getFrom());
                psts.setLong(3, time.getTo());
                psts.setInt(4, time.getSoc());
                psts.setLong(5, time.getFromlon());
                psts.setLong(6, time.getFromlat());
                psts.setLong(7, time.getTolon());
                psts.setLong(8, time.getTolat());
                psts.setString(9, time.getStatus());
                psts.addBatch();
            }
            psts.executeBatch();
            connection.commit(); //提交
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            connectionClose(connection);
        }
    }

    private void connectionClose(DruidPooledConnection connection){
        if(connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
